package com.crm.action;

import com.alibaba.fastjson.JSON;
import com.crm.dao.StudentDao;
import com.crm.model.PageBean;
import com.crm.model.role.Student;
import com.crm.util.PageUtil;
import com.crm.util.ResponseUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 1L;

    private HttpServletRequest request;
    private final StudentDao studentDao = new StudentDao();
    private Student student;
    private List<Student> studentList;
    private String error;
    private String mainPage;
    private String page;
    private int total;
    private String pageCode;
    private String title;

    /**
     * 学生登陆
     */
    public String login() {
        Student currentStudent = studentDao.login(student);
        if (currentStudent == null) {
            error = "准考证号或密码错误";
            return ERROR;
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", currentStudent);
            return SUCCESS;
        }
    }

    /**
     * 跳转到修改密码页面
     */
    public String preUpdatePassword() {
        mainPage = "business/student/updatePassword.jsp";
        return SUCCESS;
    }

    /**
     * 修改密码
     */
    public String updatePassword() {
        Student s = studentDao.getStudentById(student.getId());
        s.setPassword(student.getPassword());
        studentDao.save(s);
        mainPage = "business/student/updatePasswordSuccess.jsp";
        return SUCCESS;
    }

    /**
     * 退出系统
     */
    public String logout() {
        request.getSession().invalidate();
        return "logout";
    }

    /**
     * 学生集合
     */
    public String list() {
        if (StringUtils.isEmpty(page)) {
            page = "1";
        }
        int pageSize = 5;
        PageBean pageBean = new PageBean(Integer.parseInt(page), pageSize);
        studentList = studentDao.list(student, pageBean);
        total = studentDao.count(student);
        pageCode = PageUtil.genPagination(request.getContextPath() + "/studentAction!list", total, Integer.parseInt(page), pageSize);
        mainPage = "business/student/studentList.jsp";
        return SUCCESS;
    }

    /**
     * 修改或增加学生判断
     */
    public String preSave() {
        if (student != null) {
            student = studentDao.getStudentById(student.getId());
            title = "修改学生信息";
        } else {
            title = "添加学生信息";
        }
        mainPage = "business/student/studentSave.jsp";
        return SUCCESS;
    }

    /**
     * 修改或增加学生
     */
    public String save() {
        if (ObjectUtils.isEmpty(student.getId())) {
            student.setCardNo("ly" + DateFormatUtils.format(new Date(), "yyyyMMddhhmmss"));
        }
        studentDao.save(student);
        return "save";
    }

    /**
     * 删除学生
     */
    public void delete() throws Exception {
        student = studentDao.getStudentById(student.getId());
        studentDao.delete(student);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("success", true);
        ResponseUtil.write(JSON.toJSONString(resultMap), ServletActionContext.getResponse());
    }


    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }


    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMainPage() {
        return mainPage;
    }

    public void setMainPage(String mainPage) {
        this.mainPage = mainPage;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getPageCode() {
        return pageCode;
    }

    public void setPageCode(String pageCode) {
        this.pageCode = pageCode;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
